package com.bjpowernode.other.volitale;

/**
 * 指令重排序
 */
public class PossibleReordering {

    private static int x = 0, y = 0;
    private static int a = 0, b =0;
    static int count = 0;

    public static void main(String[] args) throws InterruptedException {

        while (true) {
            x = 0;y = 0;
            a = 0;b = 0;
            Thread one = new Thread(() -> {
                a = 1;
                x = b;
            });
            Thread two = new Thread(() -> {
                b = 1;
                y = a;
            });
            one.start();
            two.start();
            one.join();
            two.join();
            count++;
            if (x == 0 && y == 0) {
                System.out.println("第" + count + "次出现了指令重排序");
                break;
            }
        }
    }
}